#include "histogramGray.h"
#include <iostream>

HistogramGray::HistogramGray(): Histogram()
{	
    histogram=new unsigned int[256];
}

HistogramGray::HistogramGray(QImage* mi): Histogram(mi)
{
    histogram=new unsigned int[256];
}

HistogramGray::~HistogramGray()
{
    delete []histogram;
}

void HistogramGray::calcHistogram()
{
    if(ownerImage==NULL)
        return;
    else
    {
        for(int i=0;i<256;i++)
        {
            histogram[i]=0;
        }
        for(int x=0;x<ownerImage->width();x++)
            for(int y=0;y<ownerImage->height();y++)
                histogram[qGray(ownerImage->pixel(x,y))]++;
        for(int i=0;i<256;i++)
        {
            std::cout << i << ". " << histogram[i] << std::endl;
        }
        calcHistImage();
    }
}

void HistogramGray::calcHistImage()
{
    int width=512;
    int height=300;
    int fracc=width/256;
    if(histImage==NULL)
        histImage=new QImage(width,height,QImage::Format_RGB32);
    unsigned long max=0;
    for(int i=0;i<256;i++)
        if(histogram[i]>max)
            max=histogram[i];
    for(int x=0;x<width;x++)
        for(int y=0;y<height;y++)
            histImage->setPixel(x,y,qRgb(0,0,0));
    for(int i=0;i<width;i++)
    {
        int h=(height-1)*(1.0-histogram[i/fracc]/(double)max);
            histImage->setPixel(i,h,qRgb(255,255,255));
            for(int j=h+1;j<height;j++)
                histImage->setPixel(i,j,qRgb(128,128,128));

    }
}
